package com.ssy.lingxi.contract.esign.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.WordsPositionSearchRequest;
import com.ssy.lingxi.contract.esign.response.WordsPositionSearchResponse;

import java.util.List;
import java.util.Map;

/**
 * 辅助接口
 * @author yzc
 * @version 2.0.0
 * @date 2020/8/26
 */
public class Helper {

    private final AuthToken authToken;
    private final EsignProperties properties;

    public Helper(AuthToken authToken, EsignProperties properties) {
        this.authToken = authToken;
        this.properties = properties;
    }

    /**
     * 搜索关键字坐标
     */
    public List<WordsPositionSearchResponse> wordsPositionSearch(WordsPositionSearchRequest request) throws EsignException {
        String resourcePath = "/v1/documents/{fileId}/searchWordsPosition";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, new TypeReference<List<WordsPositionSearchResponse>>() {
        }, false);
    }
}